/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.util;

import wf.dto.tbtwtaut;
import wf.dto.tccuenta;
import acceso.hibernate.AccesoHibernate;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import twitter4j.Twitter;

/**
 *
 * @author romulogalindo
 */
public class Twitter_task extends java.util.TimerTask{
    Twitter twitter;
    List ls = new ArrayList();
    public Twitter_task() {
    }
    
    @Override
    public void run() {
        StatelessSession HSESSION=null;
        try{
            List l = null;
            try{
                HSESSION=AccesoHibernate.getSessionFactory().openStatelessSession();
                //Transaction TRANSAX = HSESSION.beginTransaction();
                //TRANSAX.begin();
                Query q = HSESSION.getNamedQuery("twtaut.tbtwtaut");

                 l= q.list();

                //TRANSAX.commit();
                //HSESSION.close();
            }catch(Exception ep){
                throw ep;
            }finally{
                close(HSESSION);
            }
            
            if(l != null){
                //System.out.println("Cant. de mensajes por enviar"+l.size());
                for(int i = 0 ; i < l.size();i++){
                    tbtwtaut twt = (tbtwtaut)l.get(i);
                    procesa_twitter(twt);
                }

            }
        }catch(Exception ep){
            System.out.println("[TAREA: TWITTER] "+ep.getMessage());
        }
    }
    
    public void procesa_twitter(tbtwtaut twt){
        OTwitter t = null;
        for(int i = 0 ; i < ls.size();i++){
            t = (OTwitter)ls.get(i);
            if(twt.getCo_cuenta() == t.getId()){
                i = ls.size()-1;
            }else{
                t = null;
            }
        }
        
        if(t == null){
            StatelessSession HSESSION = null;
            tccuenta cuenta = null;
            try{
                HSESSION=AccesoHibernate.getSessionFactory().openStatelessSession();
                //Transaction TRANSAX = HSESSION.beginTransaction();
                //TRANSAX.begin();
                Query q = HSESSION.getNamedQuery("twtaut.tccuenta");
                q.setInteger("id_cuenta", twt.getCo_cuenta());

                cuenta = (tccuenta) q.uniqueResult();

                //TRANSAX.commit();
                //HSESSION.close();
            }catch(Exception ep){
                //throw ep;
            }finally{
                close(HSESSION);
            }

            if(cuenta != null){
                OTwitter tn = new OTwitter(cuenta);
                ls.add(tn);
                tn.publicar(twt);
            }
        }else{
            if(t.conectado){
                t.publicar(twt);
            }else{
                StatelessSession HSESSION=null;
                tccuenta cuenta = null ;
                try{
                    HSESSION=AccesoHibernate.getSessionFactory().openStatelessSession();
                    //Transaction TRANSAX = HSESSION.beginTransaction();
                    //TRANSAX.begin();
                    Query q = HSESSION.getNamedQuery("twtaut.tccuenta");
                    q.setInteger("id_cuenta", twt.getCo_cuenta());

                    cuenta = (tccuenta) q.uniqueResult();

                    //TRANSAX.commit();
                    //HSESSION.close();
                }catch(Exception ep){
                    //throw ep;
                }finally{
                    close(HSESSION);
                }
                if(cuenta != null){
                    t.reconectar(cuenta);
                }
            }
        }
        
    }
    
    public void close(StatelessSession HSESSION){
        try {            
            if (HSESSION != null) {
                if(!HSESSION.connection().isClosed()) HSESSION.close();
            }
        }catch (Exception ep) {HSESSION = null;}
    }
    
}

